package com.dxj1992.common;

import com.dxj1992.entity.SteamBalances;
import com.dxj1992.handler.QueueTaskHandler;
import com.dxj1992.handler.TestServiceHandler;
import com.dxj1992.service.ISteamBalancesService;
import com.dxj1992.service.impl.QueueGenerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.util.List;

@Component
public class FixedPrintTask {
    @Autowired
    private ServletContext servletContext;
    @Autowired
    private QueueGenerationService queueGenerationService;
    @Autowired
    private ISteamBalancesService steamBalancesService;


    /**
     * 每天凌晨4点删除首页缓存
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void execute() {
        servletContext.removeAttribute("articleTypeList");
        servletContext.removeAttribute("articleHotList");
        servletContext.removeAttribute("articleTagList");
        servletContext.removeAttribute("adIndexList");
        servletContext.removeAttribute("linkList");
    }

    /**
     * 每秒执行一次
     */
//    @Scheduled(cron = "0/5 * *  * * ? ")
//    public void priceUpdate() {
//        this.getPrice();
//    }

    /**
     * 获取价格
     */
    private String getPrice() {
        List<SteamBalances> list = steamBalancesService.list();
        list.forEach(a -> {
            try {
                Thread.sleep(1000);
                QueueTaskHandler queueTaskHandler = new TestServiceHandler(steamBalancesService, a);
                boolean b = queueGenerationService.addData(queueTaskHandler);
                System.out.println(b);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return null;
    }

}